// 页面路径：store/index.js
// import { createStore } from "vuex"
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// import { Login } from "../api/list/home/index.js";

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('satoken') || '',
	},
	mutations: {
		SET_TOKEN: (state, token) => {
			state.token = token
		},
	},
	actions: {
		// 登陆
		toLogin({
			commit
		}, info) {
			return new Promise((resolve, reject) => {
				Login(info).then(res => {
					let data = res.result
					commit('SET_TOKEN', data.token)
					commit('SET_ROLES', data.roles)
					commit('SET_ROLES_ID', data.roles[0])
					commit('SET_ORG', data.roles)
					uni.setStorageSync('satoken', data.token)
					uni.setStorageSync('roles', data.roles)
					uni.setStorageSync('rolesId', data.roles[0])
					uni.setStorageSync('org', data.org)
					resolve(res)
				}).catch(err => {
					reject(err)
				})
			})
		},

		// 登出
		toLoginOut({
			commit
		}) {
			return new Promise((resolve, reject) => {
				uni.showToast({
					title: res.message || '退出成功',
					icon: 'none'
				})
				uni.clearStorageSync()
				uni.reLaunch({
					url: '/pages/index/index'
				})
			})
		}
	},

	getters: {
		token: state => state.token,
	}
})

export default store